﻿//0.1, 20100910
//0.2, 20100911, adjust the picture height
//0.3, 20100918, add "ctrl+a" shortcut

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ConvertTextToImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            string strPath = Properties.Settings.Default.ImageFileFolder;
            if (string.IsNullOrEmpty(strPath) || Directory.Exists(strPath) == false)
            {
                strPath = Environment.CurrentDirectory;
            }
            textBoxFolder.Text = strPath;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //CultureInfo ci = Thread.CurrentThread.CurrentUICulture;

            this.Text += this.GetType().Assembly.GetName().Version.ToString();

            textBoxFontSize.Text = textBoxContent.Font.Size.ToString();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string strPath = folderBrowserDialog1.SelectedPath;
                textBoxFolder.Text = strPath;
                if (Properties.Settings.Default.ImageFileFolder != strPath)
                {
                    Properties.Settings.Default.ImageFileFolder = strPath;
                    Properties.Settings.Default.Save();
                }
            }
        }

        private void buttonConvert_Click(object sender, EventArgs e)
        {
            string strContent = Environment.NewLine + textBoxContent.Text.Trim() + Environment.NewLine;
            string strFolder = textBoxFolder.Text.Trim();
            string strFileName = string.Empty;
            Color FontColor = Color.Blue;
            Color BackColor = Color.White;
            int iImageHeight = int.MinValue;
            int iImageWidth = textBoxContent.Width;
            string strMessage = string.Empty;

            Cursor currentCursor = Cursor.Current;
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                Size sz = new Size(iImageWidth, Int32.MaxValue);
                sz = TextRenderer.MeasureText(strContent, textBoxContent.Font, sz, TextFormatFlags.WordBreak);
                iImageHeight = Convert.ToInt32(sz.Height * 1.02);

                Bitmap objBitmap = new Bitmap(iImageWidth, iImageHeight);

                Graphics objGraphics = Graphics.FromImage(objBitmap);
                Color objColor = Color.Gray; ;
                //Font objFont = new Font(strFontName, iFontSize);

                //define where the text will be displayed in the specified area of the image
                PointF objPointF = new PointF(1.0F, 1.0F);

                SolidBrush BrushForeColor = new SolidBrush(FontColor);
                SolidBrush BrushBackColor = new SolidBrush(BackColor);
                Pen BorderPen = new Pen(objColor);

                Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(iImageWidth - 1, iImageHeight - 1));
                objGraphics.FillRectangle(BrushBackColor, displayRectangle);
                objGraphics.DrawRectangle(BorderPen, displayRectangle);

                //Define string format 
                StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
                StringFormat format2 = new StringFormat(format1);
                //Draw text string using the text format
                objGraphics.DrawString(strContent, textBoxContent.Font, Brushes.Black, (RectangleF)displayRectangle, format2);

                if (strFolder.EndsWith(@"\") == false)
                {
                    strFolder += @"\";
                }
                strFileName = strFolder + DateTime.Now.ToString(@"yyyyMMdd_HHmmss") + @".gif";
                objBitmap.Save(strFileName, ImageFormat.Gif);

                string strFileDimension = string.Format("{0}*{1}", objBitmap.Width, objBitmap.Height);
                FileInfo f = new FileInfo(strFileName);
                string strFileLength = (f.Length / 1024.0).ToString("#,##0.#") + @" KB";
                CultureInfo ci = Thread.CurrentThread.CurrentUICulture;
                if (ci.Name.Equals(@"zh-CN", StringComparison.InvariantCultureIgnoreCase))
                {
                    strMessage = string.Format("文件长度 = {0}, 大小 = {1}{2}{3}", strFileLength, strFileDimension, Environment.NewLine, strFileName);
                }
                else
                {
                    strMessage = string.Format("File length = {0}, size = {1}{2}{3}", strFileLength, strFileDimension, Environment.NewLine, strFileName);
                }

                writeLog(strMessage);
            }
            catch (Exception ex)
            {
                strMessage = "Exception thrown out! Error message=" + ex.Message;
                writeLog(strMessage);
            }
            finally
            {
                Cursor.Current = currentCursor;
            }
            //MessageBox.Show(strMessage);


            if (checkBoxClipboard.Checked == true)
            {
                try
                {
                    Clipboard.Clear();
                    //Clipboard.SetDataObject(strFileName, true);
                    Clipboard.SetText(strFileName);
                }
                catch (Exception ex)
                {
                    ;   
                    //throw;
                }
            }

        }

        private void textBoxContent_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\x1')
            {
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
        }

        private void writeLog(string strMessage)
        {
            string strNow = DateTime.Now.ToString("HH:mm:ss") + @" - ";
            textBoxLog.Text = strNow + strMessage + Environment.NewLine + textBoxLog.Text;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxLog.Text = string.Empty;
        }

        private void textBoxFontSize_TextChanged(object sender, EventArgs e)
        {
        }

        private void textBoxFontSize_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)Keys.Return)
            {
                return;
            }

            string strFontSize = textBoxFontSize.Text.Trim();
            if (string.IsNullOrEmpty(strFontSize))
                return;
            int iFontSize = int.MinValue;
            if (int.TryParse(strFontSize, out iFontSize))
            {
                if (iFontSize > 0 && iFontSize < 1000)
                {
                    Font oFont = textBoxContent.Font;
                    oFont = new Font(oFont.Name, iFontSize,
                        oFont.Style, oFont.Unit,
                        oFont.GdiCharSet, oFont.GdiVerticalFont);
                    textBoxContent.Font = oFont;
                }
            }
        }
    }
}
